using System;
using System.Collections.Generic;

namespace StackExample // 命名空间
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // 创建一个 Stack
            Stack<string> stack = new Stack<string>();

            // 压栈（添加元素到栈中）
            stack.Push("Alice");
            stack.Push("Bob");
            stack.Push("Charlie");

            // 打印栈中的元素
            Console.WriteLine("Stack elements (top down):");
            foreach (var person in stack)
            {
                Console.WriteLine(person);
            }

            // 出栈（移除栈顶元素）
            string poppedPerson = stack.Pop();
            Console.WriteLine($"\nPopped: {poppedPerson}"); // 输出: Popped: Charlie

            // 查看栈顶元素，但不移除它
            string peekPerson = stack.Peek();
            Console.WriteLine($"Next in stack: {peekPerson}"); // 输出: Next in stack: Bob

            // 打印更新后的栈元素
            Console.WriteLine("\nStack elements after pop:");
            foreach (var person in stack)
            {
                Console.WriteLine(person);
            }

            // 检查栈的数量
            Console.WriteLine($"\nTotal elements in stack: {stack.Count}");

            // 清空栈
            stack.Clear();
            Console.WriteLine($"Total elements after clearing: {stack.Count}"); // 输出: 0
        }
    }
}
